import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { UserRole } from './entities/user_role.entity';
import { CreateUserDto } from '../auth/dto/register.dto';
import { UpdateUserDto } from './dto/update-user.dto';



@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User) private readonly user: Repository<User>,
    @InjectRepository(UserRole) private readonly role: Repository<UserRole>
  ) { }

  async create(createUserDto: CreateUserDto) {
    createUserDto.registerTime = new Date().toLocaleString()
    const res = await this.user.save(createUserDto);
    // console.log(res);
    return {
      status: 200,
      msg: "添加成功",
      data: null
    };
  }

  findAll() {
    return `This action returns all user`;
  }

  findOne(id: number) {
    return {
      success: true,
      data: id
    };
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    return `This action updates a #${id} user`;
  }

  remove(id: number) {
    return `This action removes a #${id} user`;
  }

  findUserByName(username: string) {
    return this.user.findOne({
      where: { username }
    })
  }
}
